import md5 from "crypto-js/md5";

const insert = (source) => {
    let sql = `INSERT INTO ${source.getTableName()} (`;
    for (const key in source) {
        if(key === 'tableName'){
            continue;
        }
        if(source[key]){
            sql += `${key}, `;
        }
    }
    sql = sql.trim();
    sql = sql.substring(0, sql.length - 1);
    sql += `) VALUES (`;
    for (const key in source) {
        if(key === 'tableName'){
            continue;
        }
        if(source[key]){
            if(typeof source[key] === 'string'){
                sql += `'${source[key]}', `;
            }else{
                sql += `${source[key]}, `;
            }
        }
    }
    sql = sql.trim();
    sql = sql.substring(0, sql.length - 1);
    sql += `)`;
    return sql;
}

const updateById = (source) => {
    let sql = `UPDATE ${source.getTableName()} SET `;
    for (const key in source) {
        if(key === 'tableName' || key === 'id'){
            continue;
        }
        if(source[key] == 0 || (source[key] != null && source[key] != "undefined" && source[key] != "")){
            if(typeof source[key] === 'string'){
                sql += `${key} = '${source[key]}', `;
            }else{
                sql += `${key} = ${source[key]}, `;
            }
        }
    }
    sql = sql.trim();
    sql = sql.substring(0, sql.length - 1);
    sql += ` WHERE id = '${source['id']}'`;
    return sql;
}

const deleteById = (source) => {
    let sql = `DELETE FROM ${source.getTableName()} WHERE id = '${source['id']}'`;
    return sql;
}

const page = (source) => {
    let sql = `SELECT * FROM ${source.tableName} `;
    for(let i = 0;i < source.entity.length;i++){
        const key = source.entity[i].name;
        const value = source.entity[i].value;
        if(!key || !value){
            continue;
        }
        const operator = source.entity[i].operator;
        if(operator === 'LIKE'){
            sql += `WHERE ${key} like '%${value}%' `;
        }else if(operator === 'EQ'){
            sql += `WHERE ${key} = '${value}' `;
        }else if(operator === 'GT'){
            sql += `WHERE ${key} > '${value}' `;
        }else if(operator === 'LT'){
            sql += `WHERE ${key} < '${value}' `;
        }
    }
    sql += `limit ${source.limit} offset ${source.offset}`;
    return sql;
}

const countByPage = (source) => {
    let sql = `SELECT IFNULL(COUNT(*), 0) counter FROM (`;
    sql += `SELECT * FROM ${source.tableName} `;
    for(let i = 0;i < source.entity.length;i++){
        const key = source.entity[i].name;
        const value = source.entity[i].value;
        const operator = source.entity[i].operator;
        if(!key || !value){
            continue;
        }
        if(operator === 'LIKE'){
            sql += `WHERE ${key} like '%${value}%' `;
        }else if(operator === 'EQ'){
            sql += `WHERE ${key} = '${value}' `;
        }else if(operator === 'GT'){
            sql += `WHERE ${key} > '${value}' `;
        }else if(operator === 'LT'){
            sql += `WHERE ${key} < '${value}' `;
        }
    }
    sql += `limit ${source.limit} offset ${source.offset}`;
    sql += `)`;
    return sql;
}

export default {
    insert,
    updateById,
    deleteById,
    page,
    countByPage
}
